package xls;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;

import java.io.*;
import java.util.Iterator;

/**
 * User: Vladimir
 * Date: 02.07.12 Time: 14:03
 */
public class POISample {
    public static void main(String[] args) throws IOException {
        InputStream fis = new FileInputStream("sklad/sells/Алтайская,16 15.12.2012.docx");
//        POIFSFileSystem fs = new POIFSFileSystem(fis);
        XWPFDocument doc = new XWPFDocument(fis);
        XWPFTable table = doc.getTables().get(1);
        XWPFWordExtractor wordxExtractor = new XWPFWordExtractor(doc);
        String text = wordxExtractor.getText();
        System.out.println();
    }
/*
    public static void main(String[] args) throws IOException {
//        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("excel/forma-113_0.xls"));
//        DirectoryEntry dir = fs.getRoot();
//        DocumentEntry document = (DocumentEntry)dir.getEntry("Workbook");
//        DocumentInputStream dstream = new DocumentInputStream(document);
//        BufferedReader reader = new BufferedReader(new InputStreamReader(dstream));
//        String line = null;
//        while((line = reader.readLine()) != null) {
//            System.out.println(line);
//        }
        InputStream myxls = new FileInputStream("excel/forma-113_template.xls");
        InputStream donorXls = new FileInputStream("excel/forma_107.xls");
        FileOutputStream fileFiltered = new FileOutputStream("excel/forma-113_filtered.xls");
        HSSFWorkbook wb = new HSSFWorkbook(myxls);
        HSSFWorkbook donorWb = new HSSFWorkbook(donorXls);
        HSSFSheet sheet = wb.getSheetAt(0);
        for (Row row : sheet) {
            for (Cell cell : row) {
                switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_STRING: {
                        if (cell.getRichStringCellValue().length() > 1) {
                            System.out.println(cell.getRichStringCellValue().getString());
                        }
                        if (cell.getRichStringCellValue().getString().contains("${rub}")) {
                            cell.setCellValue(1000 + "");
                        } else if (cell.getRichStringCellValue().getString().contains("${kop}")) {
                            cell.setCellValue("00");
                        }
                        break;
                    }
                    case Cell.CELL_TYPE_BLANK:
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        if (DateUtil.isCellDateFormatted(cell)) {
                            System.out.println(cell.getDateCellValue());
                        } else {
                            System.out.println(cell.getNumericCellValue());
                        }
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.println(cell.getBooleanCellValue());
                        break;
                    case Cell.CELL_TYPE_FORMULA:
                        System.out.println(cell.getCellFormula());
                        break;
                    default:
                        System.out.println();
                }
            }
        }
        Util.copySheets(wb.createSheet("Опись вложения"), donorWb.getSheetAt(0));
        wb.write(fileFiltered);
        fileFiltered.close();
    }
*/
}
